class Employee { int id; String name; Address homeAddress ; Address officeAddress; //Address is a class } Class Customer { String name ; Address address ; }
class Address { String city,state,country; public Address(String ct,String st,String cr) { city=ct; state=st; country=cr; } } class Customer { String name; Address address; public void display() { System.out.println(name); System.out.println(address.city); System.out.println(address.state); System.out.println(address.country); } public Customer(String name,Address ad) { this.name=name; this.address=ad; } } class demo { public static void main(String args[]) { Address adr1=new Address("lucknow","UP","india"); Customer a=new Customer("Amit Jain",adr1); a.display(); } }
Amit Jain lucknow UP india
protected void finalize() throws Throwable { //resource closing operations }
class Person { protected String name,city; public Person(String nm,String ct) { name = nm; city = ct; } protected void finalize() throws Throwable { System.out.println("Finalize Method executed"); } } class demo { public static void main(String args[]) { Person a=new Person("Amit","Amravati"); Person b=new Person("Nitin","Nagpur"); a=b; // object a will become abandoned // as a is referring to object b //Calling garbage collector thread explicitly System.gc(); System.out.println("done"); } }
done
Finalize Method executed
class MyClass { private static MyClass obj; private MyClass() { } public static MyClass getObject() { if(obj==null) { obj=new MyClass(); } return obj; } } class demo { public static void main(String args[]) { MyClass a1=MyClass.getObject(); MyClass a2=MyClass.getObject(); System.out.println(a1); System.out.println(a2); } }
MyClass@15db9742 MyClass@15db9742